﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PowerBooking.RSE.Utility.Helpers
{
    /// <summary>
    /// 表达式辅助类
    /// </summary>
    public class RegexHelper
    {
        /// <summary>
        /// 0个或者多个空格
        /// </summary>
        public static string Space = @"\s*";

        /// <summary>
        /// 可有可无的空格
        /// </summary>
        public static string _Space = @"[\s*]?";

        /// <summary>
        /// 任意Html标签
        /// </summary>
        public static string AnyHtmlDom = "<[^>]+>";

        /// <summary>
        /// 任意的属性值
        /// </summary>
        public static string AnyAttributeValue = @"[^'"">]+";

        /// <summary>
        /// 分组的任意的属性值
        /// </summary>
        public static string AnyAttributeValueInGroup = @"([^'"">]+)";

        /// <summary>
        /// 任意的值
        /// </summary>
        public static string AnyAttributes = @"[^>]+";

        /// <summary>
        /// 任意的标签值
        /// </summary>
        public static string AnyDomValue = @"[^<]+";

        /// <summary>
        /// 分组的任意的标签值
        /// </summary>
        public static string AnyDomValueInGroup = @"([^<]+)";

        /// <summary>
        /// 创建属性
        /// </summary>
        /// <param name="attributeName"></param>
        /// <param name="attributeValue"></param>
        /// <returns></returns>
        public static string CreateAttribute(string attributeName, string attributeValue)
        {
            return string.Format(@"{0}[\s*]?=[\s*]?['""]?[\s*]?{1}[\s*]?['""]?", attributeName, attributeValue);
        }

        /// <summary>
        /// 创建节点
        /// </summary>
        /// <param name="domName"></param>
        /// <param name="domAttributes"></param>
        /// <param name="domValue"></param>
        /// <returns></returns>
        public static string CreateDom(string domName, string domAttributes, string domValue, bool isMatch = true)
        {
            var parttern = string.Format(@"[\s*]?<{0}\s*{1}[\s*]?>[\s*]?{2}[\s*]?</{0}>[\s*]?", domName, domAttributes, domValue);
            return isMatch ? parttern : "[" + parttern + "]?";
        }

        /// <summary>
        /// 创建单闭合节点
        /// </summary>
        /// <param name="domName"></param>
        /// <param name="domAttributes"></param>
        /// <param name="domValue"></param>
        /// <returns></returns>
        public static string CreateDomSingleClose(string domName, string domAttributes, bool isMatch = true)
        {
            var parttern = string.Format(@"<{0}\s*{1}[\s*]?[/]?>", domName, domAttributes);
            return isMatch ? parttern : "[" + parttern + "]?";
        }

        public static string CheckStringLastChar(string charStr, string str)
        {
            var lastChar = str.Substring(str.Length - 1);
            return lastChar.Equals(charStr) ? str : str + charStr;
        }

        /// <summary>
        /// 根据属性名和值取嵌套对应标签正则
        /// </summary>
        /// <param name="attributeName"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string CreatePartternByAttributeName(string attributeName,string value)
        {
            return string.Format(@"<[\s*]?([0-9a-zA-Z]+)(?:(?!\b{0}\b)[^<>])*{0}[\s*]?=[\s*]?([""']?){1}\2[^>]*>(?><\1[^>]*>(?<o>)|</\1>(?<-o>)|(?:(?!</?\1).)*)*(?(o)(?!))</\1>", attributeName, value);
        }
    }
}
